package com.spi;

import lombok.Data;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

/**
 * @author God.Jhon.Black
 * @date 2023-01-11
 */
@Data
public class AnimalManagerLoader {
    private static final AnimalManagerLoader INSTANCE = new AnimalManagerLoader();

    private final List<AnimalSay> animalSays;

    private AnimalManagerLoader() {
        animalSays = load();
    }

    /**
     * 通过SPI加载实现类
     */
    private List<AnimalSay> load() {
        ArrayList<AnimalSay> animalSays = new ArrayList<>();
        Iterator<AnimalSay> iterator = ServiceLoader.load(AnimalSay.class).iterator();
        while (iterator.hasNext()){
            animalSays.add(iterator.next());
        }
        return animalSays;
    }

    public static AnimalManagerLoader getInstance() {
        return INSTANCE;
    }
}
